<template>
  <!-- 年终结转/上年转入 -->
  <div style="display:flex;flex-direction: column;height:100%">
    <div>
      <cus-allcard style="padding-top:20px;padding-bottom:0"
                   :showTitle="false">
        <div class="topBody">
          <div class="topBodyItem">
            <span class="itemSpan">转出年度</span>
            <Select v-model="selectFrom.accountbookId"
                    placeholder="请选择转出年度"
                    @on-change="accountbookIdChange()"
                    class="itemImport">
              <Option v-for="item in selectFrom.yearList"
                      :value="item.accountbookId"
                      :key="item.year">{{ item.year }}</Option>
            </Select>
          </div>
          <div class="topBodyItem">
            <span class="itemSpan">转入年度</span>
            <Input v-model="selectFrom.year"
                   class="itemImport"
                   :disabled="true"
                   placeholder="请输入转入年度"
                   maxlength="15"></Input>
          </div>
          <div class="topBodyItem">
            <span class="itemSpan">入账日期</span>
            <Date-picker type="date"
                         disabled
                         v-model="selectFrom.dataTime"
                         :clearable="false"
                         placeholder="请选择入账日期"
                         class="itemImport"></Date-picker>
          </div>
          <div class="topBodyItem">
            <span class="itemSpan">摘要内容</span>
            <Input v-model="selectFrom.abstractName"
                   class="itemImport"
                   :disabled="true"
                   placeholder="请输入摘要名称"
                   maxlength="15"></Input>
          </div>
          <div class="topBodyItem">
            <span class="itemSpan">备注信息</span>
            <Input v-model="selectFrom.remark"
                   class="itemImport"
                   placeholder="请输入备注信息"
                   maxlength="15"></Input>
          </div>
          <div class="topBodyItem">
            <cus-Button class="ml_20"
                        type="primary"
                        :disabled="!selectFrom.year||!selectFrom.accountbookId"
                        @onClick="tabelCarryForwardToNextButton">上年转入</cus-Button>
          </div>
        </div>
      </cus-allcard>
    </div>
    <div style="flex:1;"
         class="tableBox">
      <cus-allcard style="margin-top:20px"
                   :showTitle="false">
        <Tabs :value="tabsValue"
              style="padding-top:16px"
              @on-click="tabsValueChange($event)">
          <Tab-pane label="经费账科目"
                    name="nameOne">
            <cusTable border
                   :loading="form.loading"
                   :columns="columnsOne"
                   :data="dataOne"
                  
                   :size="vuex_config.tableSize">
              <template slot-scope="{ row }"
                        slot="recmoney">
                {{totalPrices(row.recmoney)}}
              </template>
              <template slot-scope="{ row }"
                        slot="paymoney">
                {{totalPrices(row.paymoney)}}
              </template>
            </cusTable>
          </Tab-pane>
          <Tab-pane label="粮秣账科目"
                    name="nameTwo">
            <cusTable border
                   :loading="form.loading"
                   :columns="columnsTwo"
                   :data="dataTwo"
                   :size="vuex_config.tableSize">
              <template slot-scope="{ row }"
                        slot="recrice">
                {{totalPrices(row.recrice)}}
              </template>

              <template slot-scope="{ row }"
                        slot="recflour">
                {{totalPrices(row.recflour)}}
              </template>

              <template slot-scope="{ row }"
                        slot="recoil">
                {{totalPrices(row.recoil)}}
              </template>

              <template slot-scope="{ row }"
                        slot="recbean">
                {{totalPrices(row.recbean)}}
              </template>
              <template slot-scope="{ row }"
                        slot="payrice">
                {{totalPrices(row.payrice)}}
              </template>

              <template slot-scope="{ row }"
                        slot="payflour">
                {{totalPrices(row.payflour)}}
              </template>

              <template slot-scope="{ row }"
                        slot="payoil">
                {{totalPrices(row.payoil)}}
              </template>

              <template slot-scope="{ row }"
                        slot="paybean">
                {{totalPrices(row.paybean)}}
              </template>
            </cusTable>

          </Tab-pane>
        </Tabs>

      </cus-allcard>
    </div>

  </div>
</template>
<script>
import {
  getAccountBookListByUser,
  getAccountNoCarryOver,
  carryOverLastYearExhibition,
  carryOverLastYear
} from '@/api/businessAccountingApi/essentialInformationApi'
export default {
  data () {
    return {
      tabsValue: 'nameOne', // nameOne经费账 nameTwo粮秣账
      subjecttype: 0, // 0经费账 1粮秣账
      form: {
        loading: false,
        name: '',
        startTime: '',
        endTime: ''
      },
      selectFrom: {
        year: null, // 账簿年份
        yearList: [], // 账簿年份列表
        accountbookId: null, // 转出年度id
        accountBookLastId: null, // 转入年度id
        dataTime: null, // 入账日期
        abstractName: '上年转入', // 摘要内容
        remark: null, // 备注信息
        subjectDtoList: []// 列表，经费账粮秣账合并
      },
      dataOne: [], // 经费账-表格列表
      // 经费账-表头
      columnsOne: [

        {
          title: '科目',
          key: 'subjectname',
          align: 'center',
          minWidth: 100
        },
        {
          title: '收方',
          slot: 'recmoney',
          align: 'right',
          minWidth: 100
        },
        {
          title: '付方',
          slot: 'paymoney',
          align: 'right',
          minWidth: 100
        }
      ],

      dataTwo: [], // 粮秣账-表格列表
      // 粮秣账-表头
      columnsTwo: [
        {
          title: '科目',
          key: 'subjectname',
          align: 'center',
          minWidth: 120
        },
        {
          title: '收方(大米)',
          slot: 'recrice',
          align: 'right',
          minWidth: 110
        },
        {
          title: '收方(小麦粉)',
          slot: 'recflour',
          align: 'right',
          minWidth: 120
        },
        {
          title: '收方(食油/千克)',
          slot: 'recoil',
          align: 'right',
          minWidth: 140
        },
        {
          title: '收方(大豆/千克)',
          slot: 'recbean',
          align: 'right',
          minWidth: 140
        },

        {
          title: '付方(大米)',
          slot: 'payrice',
          align: 'right',
          minWidth: 110
        },
        {
          title: '付方(小麦粉)',
          slot: 'payflour',
          align: 'right',
          minWidth: 120
        },
        {
          title: '付方(食油/千克)',
          slot: 'payoil',
          align: 'right',
          minWidth: 140
        },
        {
          title: '付方(大豆/千克)',
          slot: 'paybean',
          align: 'right',
          minWidth: 140
        }
      ],
      timeOptions: {
        disabledDate (date) {
          return (
            date < new Date(new Date().Format('yyyy') - 1 + '-12-31') || date > new Date(new Date().Format('yyyy-MM-dd'))
          )
        }
      },
   

    }
  },
  mounted () {


    // 函数时提示变成3秒消失
    this.$Message.config({
      top: 50,
      duration: 3
    })
    this.getAccountNoCarryOverApi() // 根据用户id查询账簿-选择器使用 接口
  },
  // 销毁函数时提示变回1.5秒消失
  beforeDestroy () {
    this.$Message.config({
      top: 50,
      duration: 1.5
    })
  },

  methods: {
    // 千位分割符
    totalPrices (recmoney) {
      const intPart = Math.trunc(recmoney)//整数部分
      let floatPart = 0
      const intPartFormat = intPart.toString().replace(/\B(?=(\d{3})+$)/g, ',')
      const valuArray = Number(recmoney).toString().split('.')

      if (valuArray.length === 2) {//有小数部分
        if (valuArray[1]) {
          if (valuArray[1].length === 1) {
            valuArray[1] += '0'
          }
        } else {
          valuArray[1] = '00'
        }
        floatPart = valuArray[1]//取得小数部分

        return intPartFormat + '.' + floatPart
      } else {
        return intPartFormat + '.00'
      }
    },

    // 根据用户id查询账簿-选择器使用 接口
    getAccountBookListByUserApi () {
      this.form.loading = true
      return new Promise((resolve) => {
        getAccountBookListByUser({
          userid: this.vuex_user.base.id // 用户id
        }).then((res) => {
          this.form.loading = false
          if (res.errorcode === 0) {
            resolve()
          } else {
            this.$Message.error(res.resultmsg)
          }
        })
      })
    },
    // 根据用户id查询账簿-选择器使用 接口
    getAccountNoCarryOverApi () {
      this.form.loading = true
      this.selectFrom.year = ''
      this.selectFrom.yearList = []
      this.selectFrom.accountbookId = ''
      this.selectFrom.remark = ''
      this.dataOne = []
      this.dataTwo = []
      return new Promise((resolve) => {
        getAccountNoCarryOver({
          userid: this.vuex_user.base.id // 用户id
        }).then((res) => {
          this.form.loading = false
          if (res.errorcode === 0) {
            res.accountBook.forEach(element => {
              if (element.isjiezhuan === 0) {
                this.selectFrom.year = element.year // 账簿年度
                this.selectFrom.dataTime = new Date((this.selectFrom.year + '-01-01')) // 入账日期 默认为存在日期的那一年1月1日
                this.selectFrom.accountBookLastId = element.accountbookId // 账簿id
              } else if (element.isjiezhuan === 1) {
                this.selectFrom.yearList.push(element)
                this.selectFrom.accountbookId = this.selectFrom.yearList[0].accountbookId // 默认拿第一个
              }
            })
            this.carryOverLastYearExhibitionApi() // 查看结转明细 接口
            resolve()
          } else {
            this.$Message.error(res.resultmsg)
          }
        })
      })
    },
    // 上年转入明细查询 接口
    carryOverLastYearExhibitionApi () {
      this.form.loading = true
      return new Promise((resolve) => {
        carryOverLastYearExhibition({
          accountBookId: this.selectFrom.accountbookId // 账簿id
        }).then((res) => {
          this.form.loading = false
          if (res.errorcode === 0) {
            this.dataOne = res.feeCerList
            this.dataTwo = res.stapleCerList
            this.selectFrom.subjectDtoList = []
            this.dataOne.forEach(element => {
              element.type = 0
              element.subjectCode = element.subjectcode
              element.subjectName = element.subjectname
              element.recMoney = element.recmoney
              element.payMoney = element.paymoney

              this.selectFrom.subjectDtoList.push(element)
            })
            this.dataTwo.forEach(element => {
              element.type = 1
              element.subjectCode = element.subjectcode
              element.subjectName = element.subjectname
              this.selectFrom.subjectDtoList.push(element)
            })
            resolve()
          } else {
            this.$Message.error(res.resultmsg)
          }
        })
      })
    },
    // 上年转入 接口
    carryOverLastYearApi () {
      this.form.loading = true
      return new Promise((resolve) => {
        carryOverLastYear({
          accountBookId: this.selectFrom.accountbookId, // 转出账簿id
          accountBookLastId: this.selectFrom.accountBookLastId, // 转入账簿ID
          abstractName: this.selectFrom.abstractName, // 摘要
          remark: this.selectFrom.remark, // 备注
          operatorId: this.vuex_user.base.id, // 用户ID
          operatorName: this.vuex_user.base.name, // 用户名
          certificateDate: new Date(this.selectFrom.dataTime).Format('yyyy-MM-dd') // 入账时间
        }).then((res) => {
          this.form.loading = false
          if (res.errorcode === 0) {
            this.$Message.success(res.resultmsg)
            this.getAccountNoCarryOverApi()// 根据用户id查询账簿-选择器使用 接口
            resolve()
          } else {
            this.$Message.error(res.resultmsg)
          }
        })
      })
    },
    // 表格上年转入按钮
    tabelCarryForwardToNextButton () {
      if (this.selectFrom.abstractName === '' || this.selectFrom.abstractName === undefined || this.selectFrom.abstractName === null) {
        this.$Message.success('摘要内容为空，请先填写再点击上年转入')
        return
      }
      this.$cu.dialog({}).then(() => {
        this.carryOverLastYearApi() // 上年转入 接口
      })
    },
    // 账簿年度改变回调
    accountbookIdChange () {
      this.selectFrom.yearList.forEach(element => {
        if (element.accountbookId === this.selectFrom.accountbookId) {
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.topBody {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .topBodyItem {
    display: flex;
    align-items: center;
    margin-right: 12px;
    margin-bottom: 20px;
    .itemSpan {
      white-space: nowrap;
      width: 80px;
      text-align: right;
      font-size: 14px;
      color: rgba(0, 0, 0, 0.85) !important;
      margin-right: 10px;
    }
    .itemImport {
      width: 200px;
    }
  }
}
</style>
